package com.jia.train.seat.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jia.train.global.response.BaseResponse;
import com.jia.train.seat.request.ConfirmOrderDoRequest;
import com.jia.train.seat.service.ConfirmOrderService;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/confirm/order")
public class ConfirmOrderController {

    public static final Logger logger = LoggerFactory.getLogger(ConfirmOrderController.class);

    private final ConfirmOrderService confirmOrderService;

    public ConfirmOrderController(ConfirmOrderService confirmOrderService) {
        this.confirmOrderService = confirmOrderService;
    }

    @PostMapping("/do")
    public BaseResponse<String> doConfirmOrder(@Valid @RequestBody ConfirmOrderDoRequest request) {
        confirmOrderService.doConfirm(request);
        return new BaseResponse<>("success");
    }

    @PostMapping("/sync/do")
    public BaseResponse<String> doSyncConfirmOrder(@Valid @RequestBody ConfirmOrderDoRequest request) {
        confirmOrderService.doSyncConfirm(request);
        return new BaseResponse<>("sync success");
    }

    @PostMapping("/cache/do")
    public BaseResponse<String> doCacheConfirmOrder(@Valid @RequestBody ConfirmOrderDoRequest request) {
        confirmOrderService.doCacheConfirm(request);
        return new BaseResponse<>("cache Success");
    }

    @PostMapping("/dog/do")
    public BaseResponse<String> doDogConfirmOrder(@Valid @RequestBody ConfirmOrderDoRequest request) {
        confirmOrderService.doDogConfirm(request);
        return new BaseResponse<>("dog Success");
    }

    @PostMapping("/red/do")
    public BaseResponse<String> doRedConfirmOrder(@Valid @RequestBody ConfirmOrderDoRequest request) {
        confirmOrderService.doRedConfirm(request);
        return new BaseResponse<>("red Success");
    }

    @SentinelResource(value = "/confirm-sentinel-do", blockHandler = "doSentinelConfirmBlock")
    @PostMapping("/sentinel/do")
    public BaseResponse<String> doSentinelConfirmOrder(@Valid @RequestBody ConfirmOrderDoRequest request) {
        confirmOrderService.doSentinelConfirm(request);
        return new BaseResponse<>("sentinel Success");
    }

    public BaseResponse<String> doSentinelConfirmBlock(ConfirmOrderDoRequest request, BlockException e) {
        logger.info("controller 触发 doSentinelConfirmBlock.......:{}", e.getMessage());
        return new BaseResponse<>("sentinel flow Success");
    }

}
